package top.fyukina.fyson.runtime.loader;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import top.fyukina.fyson.runtime.exception.FysonException;

public final class CmdLoader {
    private CmdLoader() {}
    public static void load(String cmd, Object arg) throws FysonException {
        switch (cmd) {
            case "exit" -> System.exit(arg instanceof Integer arg0 ? arg0 : 0);
            case "print" -> {
                if (arg instanceof String arg0)
                    System.out.print(arg0);
                else if (arg instanceof JSONArray arg0)
                    for (Object a : arg0)
                        System.out.print(a instanceof JSONObject a0 ? ValueLoader.load(a0) : a);
                else if (arg instanceof JSONObject arg0)
                    System.out.print(ValueLoader.load(arg0));
            }
            case "println" -> {
                load("print", arg);
                load("print", "\n");
            }
            case "repeat" -> {
                if (arg instanceof JSONObject arg0) {
                    JSONArray s = arg0.getJSONArray("scripts");
                    if (s == null || s.size() == 0) return ;
                    if (arg0.containsKey("while")) {
                        Object w = arg0.get("while");
                        if (w instanceof JSONObject w0)
                            while (ValueLoader.ifLoad(w0))
                                JSONLoader.loadScript(s);
                        else if (w instanceof JSONArray w0)
                            while (ValueLoader.ifLoad(w0))
                                JSONLoader.loadScript(s);
                    } else if (arg0.containsKey("time")) {
                        int t = arg0.getIntValue("time");
                        if (t <= 0) return ;
                        for (int i = 1; i <= t; i++)
                            JSONLoader.loadScript(s);
                    }
                }
            }
            case "var" -> {
                if (arg instanceof JSONObject arg0) {
                    String name = arg0.getString("new");
                    if (name != null) {
                        if (ValueLoader.vars.containsKey(name)) return ;
                        ValueLoader.vars.put(name, arg0.get("val"));
                        return ;
                    }
                    name = arg0.getString("set");
                    if (name != null) {
                        if (!ValueLoader.vars.containsKey(name) || !arg0.containsKey("val"))
                            return ;
                        ValueLoader.vars.replace(name, arg0.get("val"));
                        return ;
                    }
                    name = arg0.getString("add");
                    if (name != null) {
                        if (!ValueLoader.vars.containsKey(name) || !(ValueLoader.vars.get(name) instanceof
                                Number v) || !arg0.containsKey("val") || !(arg0.get("val") instanceof Number v0))
                            return ;
                        ValueLoader.vars.replace(name, v.doubleValue() + v0.doubleValue());
                    }
                }
            }
            case "fun" -> {

            }
        }
    }
}